package com.kurancy.web.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.kurancy.service.KurancyUserAction;

@RequestMapping("/kurancycreateds")
@Controller
//@RooWebScaffold(path = "kurancycreateds", formBackingObject = KurancyCreated.class)
public class KurancyCreatedController {
	
	private static Log logger = LogFactory.getLog(KurancyCreatedController.class);
	
	@Autowired
	private KurancyUserAction kurancyUserAction;
	
	 @RequestMapping(produces = "text/html")
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
        if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            
            uiModel.addAttribute("kurancycreateds", kurancyUserAction.findKurancyCreatedEntries(firstResult, sizeNo));
            float nrOfPages = (float) kurancyUserAction.countAllKurancyCreateds() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            uiModel.addAttribute("kurancycreateds", kurancyUserAction.findAllKurancyCreateds());
        }

        return "kurancycreateds/list";
    }
}
